package com.semidata.trp.business.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.semidata.rtd.core.service.SendableService;
import com.semidata.trp.business.config.Config;
import com.semidata.trp.message.business.Business;
import com.semidata.trp.message.business.Business.BusinessData;

public class BusinessDataDebugService implements SendableService<Business.BusinessData> {
    
    private LinkedBlockingQueue<Business.BusinessData> queue = new LinkedBlockingQueue<>();
    private Thread thread = null;
    private boolean quit = false;

    @Override
    public void start() {
        thread = new Thread(new Runnable() {

            @Override
            public void run() {
                
                while (!quit) {
                    FileOutputStream out = null;
                    try {
                        
                        Business.BusinessData data = queue.poll(100, TimeUnit.MILLISECONDS);
                        
                        if (data != null) {
                        
                            File dir = new File(Config.getConfig().getDebugDataDir());
                            
                            if (!dir.exists())
                                dir.mkdirs();
                            
                            if (!dir.isDirectory() || !dir.canWrite()) {
                                break;
                            }
                            
                            String filename = "data." + System.currentTimeMillis();
                            
                            File file = new File(dir, filename);
                            
                            out = new FileOutputStream(file);
                            out.write(data.toString().getBytes());
                            
                        }
                        
                        
                    } catch (Exception e) {
                       
                    } finally {
                        if (out != null)
                            try {
                                out.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    }
                    
                }
                
            }
            
        });
        
        thread.start();
        
    }

    @Override
    public void stop() {
        quit = true;
        
        if (thread != null)
            try {
                thread.join();
            } catch (InterruptedException e) {
                
            }
        
    }

    @Override
    public void send(BusinessData event) {
        if (Config.getConfig().isLogDebugEnabled()) {
            queue.add(event);
        }
        
    }

}
